package com.uh.microservice.core.util;

import java.util.concurrent.*;

public class ThreadPool {
	    static private ThreadPool mApp;
	    private static ThreadPoolExecutor pool;
	    static synchronized public ThreadPool getInstance() {
	    if (mApp == null) {
	     mApp = new ThreadPool();
	    }
	    return mApp;
	    }   
		public ThreadPool(){
	    	 //实现自定义接口
	        pool = new ThreadPoolExecutor(200, 600, 6000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5),
	        //创建策略
	        new ThreadFactory() {
			@Override
	        public Thread newThread(Runnable r) {
	                //System.out.println("线程"+r.hashCode()+"创建");
	                //线程命名
	                Thread th = new Thread(r,"threadPool"+r.hashCode());
	                return th;
	            }
	        }, 
	        //CallerRunsPolicy策略\ 切口策略
	        new ThreadPoolExecutor.CallerRunsPolicy()) {
				@Override
	            protected void beforeExecute(Thread t,Runnable r) {
	                //System.out.println("准备执行：");
	            }
				@Override
	            protected void afterExecute(Runnable r,Throwable t) {
	                //System.out.println("执行完毕：");
	            }
				@Override
	            protected void terminated() {
	                //System.out.println("线程池退出");
	            }
	        };
	    }	    	
		public  ThreadPoolExecutor getPool() {
				return pool;
		}
		public static void setPool(ThreadPoolExecutor pool) {
				ThreadPool.pool = pool;
		}
}